Skip to content

Implement Iterator protocol and resource management for CSV readers#642

Merged
laughingman7743 merged 2 commits intomasterfrom
feature/reader-iterator-protocol
Jan 4, 2026
Merged

Implement Iterator protocol and resource management for CSV readers#642
laughingman7743 merged 2 commits intomasterfrom
feature/reader-iterator-protocol

Conversation

@laughingman7743
Copy link
Member

Summary

  • CSV readers now properly implement collections.abc.Iterator protocol
  • Added close() method and context manager support for proper resource management
  • Simplified ResultSet by delegating file lifecycle to readers

Changes

pyathena/s3fs/reader.py

  • DefaultCSVReader and AthenaCSVReader now inherit from Iterator
  • Added close() method to release file resources (uses _file = None pattern, no flag needed)
  • Added __enter__/__exit__ for context manager support
  • __next__ checks if closed before reading

pyathena/s3fs/result_set.py

  • Removed _csv_file instance variable (reader now manages file lifecycle)
  • close() now calls reader.close() instead of direct file close

Test plan

  • make chk passes (lint, format, type check)
  • CI tests pass

🤖 Generated with Claude Code

- DefaultCSVReader and AthenaCSVReader now inherit from collections.abc.Iterator
- Added close() method to properly release file resources
- Added context manager support (__enter__/__exit__)
- Updated ResultSet to use reader's close() method (no exception suppression)
- Removed _csv_file instance variable from ResultSet (reader manages file lifecycle)
- Removed unnecessary contextlib.suppress (length check already handles empty files)
- Added unit tests for Iterator protocol, close(), and context manager
- No _closed flag needed - use _file is None check instead

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@laughingman7743 laughingman7743 force-pushed the feature/reader-iterator-protocol branch from 4868480 to a2c3c73 Compare January 4, 2026 16:49
@laughingman7743 laughingman7743 marked this pull request as ready for review January 4, 2026 17:42
@laughingman7743 laughingman7743 merged commit c9b0032 into master Jan 4, 2026
9 of 10 checks passed
@laughingman7743 laughingman7743 deleted the feature/reader-iterator-protocol branch January 4, 2026 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant